package com.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.common.Common;
import com.common.PageUtils;
import com.common.ReturnCode;
import com.dao.CustomerDao;
import com.dao.SysOperateLogDao;
import com.dao.TransferRecdDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.Customer;
import com.model.ReponseDto;
import com.model.SysOperateLog;
import com.service.ICustomerService;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private SysOperateLogDao sysOperateLogDao;

    @Autowired
    private TransferRecdDao transferRecdDao;

    @Override
    public ReponseDto<PageInfo> list(String uid, String userName, String invitationCode, String pageNo,Integer pageSize) {
        if(null == pageSize) {
            pageSize = PageUtils.pageSize;
        }
        Page page = PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("uid", uid);
        parMap.put("userName", userName);
        parMap.put("invitationCode", invitationCode);
        customerDao.get(parMap);
        PageInfo info = new PageInfo<>(page.getResult());
        return new ReponseDto<>(info);

    }

    @Override
    public List<Customer> search(String userName) {
        return customerDao.search(userName);
    }

    @Override
    public ReponseDto<String> edit(Customer customer, String userId, String userName) {
        if (null == customer.getId() || null == customer.getFatherId() || null == customer.getVipLevel()
                || StringUtils.isEmpty(customer.getMobile())) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        customerDao.edit(customer);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
        sysOperateLog.setDescription("用户管理-编辑用户");
        sysOperateLog.setDataCcope("用户管理-编辑用户");
        sysOperateLogDao.insert(sysOperateLog);
        return new ReponseDto<>();
    }

    @Override
    public ReponseDto<PageInfo> flowlist(Integer userId, Date startDate, Date endDate, String thirdOrderNo,
            String pageNo, Integer pageSize) {
        if (null == userId) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }

        if(null == pageSize) {
            pageSize = PageUtils.pageSize;
        }
        Page page = PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("userId", userId);
        parMap.put("startDate", startDate);
        parMap.put("endDate", endDate);
        parMap.put("thirdOrderNo", thirdOrderNo);
        transferRecdDao.get(parMap);
        PageInfo info = new PageInfo<>(page.getResult());
        return new ReponseDto<>(info);
    }

    @Override
    public ReponseDto<PageInfo> relationlist(Integer userId, String uid, String searchType, String pageNo, Integer pageSize) {
        if(StringUtils.isEmpty(uid) && userId == null) {
            return new ReponseDto<>(new PageInfo<>());
        }
        if(null == pageSize) {
            pageSize = PageUtils.pageSize;
        }
        Page page = PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<Customer> dataList = new ArrayList<>();
        
        // 1:向上查询,2:向下查询
        if ("1".equals(searchType)) {
            Map<String, Object> parMap = new HashMap<>();
            parMap.put("id", userId);
            parMap.put("uid", uid);
            List<Customer> clist = customerDao.getFather(parMap);
            if(!CollectionUtils.isEmpty(clist)) {
                
//                for (Customer c : clist) {
//                    Map<String, Object> cmap = new HashMap<>();
//                    cmap.put("id", c.getId());
//                    List<Customer> flist = customerDao.get(cmap);
//                    if (!CollectionUtils.isEmpty(flist)) {
//                        dataList.add(flist.get(0));
//                    }
//                }
                
                selectup(dataList, clist);
            }
            
        } else {
            // 向下查询
            Map<String, Object> parMap = new HashMap<>();
            parMap.put("fatherId", userId);
            parMap.put("uid", uid);
            
            List<Customer> selectList = customerDao.get(parMap);
            if(!CollectionUtils.isEmpty(selectList)) {
                if(StringUtils.isEmpty(uid)) {
                    dataList.addAll(selectList);
                }
                
                selectDown(dataList, selectList);
            }
        }
        if(CollectionUtils.isEmpty(dataList)) {
            new ReponseDto<>(new PageInfo<>(dataList));
        }
        return new ReponseDto<>(new PageInfo<>(dataList));
    }
    
    private void selectup(List<Customer> dataList, List<Customer> selectList) {
        for (Customer c : selectList) {
            Map<String, Object> cmap = new HashMap<>();
            cmap.put("id", c.getFatherId());
            List<Customer> flist = customerDao.get(cmap);
            if (!CollectionUtils.isEmpty(flist)) {
                dataList.addAll(flist);
                if(null == flist.get(0).getFatherId() || flist.get(0).getId().intValue() == flist.get(0).getFatherId().intValue()) {
                    continue;
                }
                selectup(dataList, flist);
            }
        }
    }
    
    /**
     * 递归向下查询
     * @param dataList
     * @param selectList
     */
    private void selectDown(List<Customer> dataList, List<Customer> selectList) {
        for(Customer s : selectList) {
            Map<String, Object> parMap = new HashMap<>();
            parMap.put("fatherId", s.getId());
            List<Customer> chdlist = customerDao.get(parMap);
            if(!CollectionUtils.isEmpty(chdlist)) {
                dataList.addAll(chdlist);
                selectDown(dataList, chdlist);
            }
        }
        
    }
}
